package Exception;

public class Exception1 {
    public static void main(String[] args) {
        int[] arr={1,2,3};
        //int e = getElement(arr,3);
        //出现异常，因为数组没第3号元素
        /*Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
	at Exception.Exception.getElement(Exception.java:11)
	at Exception.Exception.main(Exception.java:6)
         */
        //getElement(null,3);
        //Exception in thread "main" java.lang.NullPointerException: 传递数组的值为空
        getElement(arr,3);
        //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 索引的位置超出数组范围

    }
    public static int getElement(int[] arr,int index)
    {
        if(arr==null)
        {
            throw new NullPointerException("传递数组的值为空");
        }
        if(index<0||index>arr.length-1)
        {
            throw new ArrayIndexOutOfBoundsException("索引的位置超出数组范围");
        }
        int ele = arr[index];
        return ele;
    }
}
